package cn.yj.core.business.service;

import cn.yj.cgs.dba.mapper.SchoolMapper;
import cn.yj.commons.utils.PinyinUtils;
import cn.yj.commons.utils.StringUtils;
import cn.yj.core.business.service.iservice.ISchoolAddress;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *
 * </p>
 *
 * @author 永健
 * @since 2019-09-12 12:11
 */
@Service
public class SchoolAddressService implements ISchoolAddress, InitializingBean {

    @Resource
    private SchoolMapper schoolMapper;

    private static List<String> SCHOOL_LIST = new ArrayList<>();
    private static List<Map<String, Object>> MAPS = new ArrayList<>();

    @Override
    public List<String> listSchoolName(String name) {
        if (SCHOOL_LIST.isEmpty()) {
            SCHOOL_LIST.addAll(schoolMapper.listSchoolName(null));
        }
        if (StringUtils.isBlank(name)) {
            return SCHOOL_LIST;
        }
        return SCHOOL_LIST.stream().filter(s -> {
            if (s.contains(name)) {
                System.out.println(s);
                return true;
            }
            return false;
        }).collect(Collectors.toList());
    }

    @Override
    public List<Map<String, Object>> listSchoolMap(String name) {
        if (StringUtils.isBlank(name)){
            return MAPS;
        }
        List<String> strings = listSchoolName(name);
        Map<String, Object> map = PinyinUtils.screenManufacturer(strings);
        List<Map<String, Object>> maps = new ArrayList<>();
        map.forEach((key, val) -> {
            Map<String, Object> stringObjectHashMap = new HashMap<>();
            if (!((List<String>) val).isEmpty()) {
                stringObjectHashMap.put("key", key);
                stringObjectHashMap.put("value", val);
                maps.add(stringObjectHashMap);
            }
        });
        return maps;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        SCHOOL_LIST.addAll(schoolMapper.listSchoolName(null));
        Map<String, Object> map = PinyinUtils.screenManufacturer(SCHOOL_LIST);
        map.forEach((key, val) -> {
            Map<String, Object> stringObjectHashMap = new HashMap<>();
            if (!((List<String>) val).isEmpty()) {
                stringObjectHashMap.put("key", key);
                stringObjectHashMap.put("value", val);
                MAPS.add(stringObjectHashMap);
            }
        });
    }
}
